package com.kye.express.business.home.productintroduction;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.web.KyeWebActivity;
import com.kye.express.databinding.ItemIncrementserviceBinding;
import com.kye.model.bean.IncrementServiceModel;

import io.reactivex.functions.Consumer;

class IncrementServiceItemViewBinder extends me.drakeet.multitype.ItemViewBinder<IncrementServiceModel, IncrementServiceItemViewBinder.ViewHolder> {

    private static Context mContext;

    public IncrementServiceItemViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_incrementservice, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull IncrementServiceModel item) {
        holder.bindData(item);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private IncrementServiceModel data;
        ItemIncrementserviceBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(IncrementServiceModel item) {
            data = item;
            dataBinding.ivIcon.setImageResource(data.getIconResId());
            dataBinding.tvTitle.setText(data.getTitle());
            if (getAdapterPosition() == 0 || getAdapterPosition() == 2 || getAdapterPosition() == 6) {
                dataBinding.line.setVisibility(View.VISIBLE);
            } else {
                dataBinding.line.setVisibility(View.GONE);
            }
            RxView.clicks(dataBinding.getRoot())
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {

                        @Override
                        public void accept(Object o) throws Exception {
                            KyeWebActivity.startActivity(mContext, data.getUrl(), data.getTitle(), false);
                        }
                    });
        }
    }
}
